//
//  LoginController.m
//  GuShiNews
//
//  Created by huachun on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "LoginController.h"
#import "HiRegionController.h"
#import "HLoginViewModel.h"

@interface LoginController ()<UITextFieldDelegate,UINavigationControllerDelegate,LOGINAPPDelegate>

@property (nonatomic, strong) UIView      *Bgview;
@property (nonatomic, strong) UITextField *textname;
@property (nonatomic, strong) UITextField *textpwd;
@property (nonatomic, strong) UIButton    *Donebutton;

@property (nonatomic, strong) UIButton    *forgetbutton;
@property (nonatomic, strong) UIButton    *regionbutton;

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    [self loadtext_view];
    self.view.backgroundColor = LineColor;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:[JonesButtonItem PublicBarButtonItemPian], [JonesButtonItem NavImgButton:[UIImage imageNamed:@"back_new"] andaction:@selector(goback) andtarget:self], nil];
}

- (void)goback{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)loadtext_view{
    __weak __typeof(&*self)weakSelf = self;
    
    _Bgview = [[UIView alloc] initWithFrame:CGRectMake(0, NAV_Height + 30, RECRWITCH, 450)];
    [self.view addSubview:_Bgview];
    
    NSArray *linarray = [NSArray arrayWithObjects:@"您的手机号",@"您的密码", nil];
    [linarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *textbg            = [[UIView alloc] initWithFrame:CGRectMake(25, 70*idx, RECRWITCH-50, 50)];
        textbg.layer.cornerRadius  = 6;
        textbg.layer.masksToBounds = YES;
        textbg.backgroundColor    = [UIColor whiteColor];
        [weakSelf.Bgview addSubview:textbg];
        
        UITextField *textfield    = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, textbg.frame.size.width-20, textbg.frame.size.height-10)];
        textfield.textColor       = [UIColor blackColor];
        textfield.font            = [UIFont systemFontOfSize:17];
        [textfield setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        textfield.backgroundColor = [UIColor whiteColor];
        textfield.placeholder     = obj;
        textfield.tag             = idx;
        textfield.delegate        = self;
        textfield.layer.borderWidth = 0;
        [textbg addSubview:textfield];
        if (idx==0) {
            self.textname             = textfield;
            self.textname.keyboardType = UIKeyboardTypePhonePad;
        }else{
            self.textpwd              = textfield;
            self.textpwd.secureTextEntry = YES;
        }
    }];
    
    NSArray *buttonarray = [NSArray arrayWithObjects:@"登 录",@"忘记密码？",@"注册",nil];
    [buttonarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button           = [[UIButton alloc] initWithFrame:CGRectMake(25, 70*2+5, RECRWITCH-50, 43)];
        button.tag                 = idx+10;
        button.layer.cornerRadius  = 6;
        button.layer.masksToBounds = YES;
        [button setTitle:obj forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
        [button addTarget:self action:@selector(go_done:) forControlEvents:UIControlEventTouchUpInside];
        [weakSelf.Bgview addSubview:button];
        
        if (idx==0) {
            self.Donebutton = button;
            [self.Donebutton setBackgroundImage:[JonesClass createImageWithColor:Buttoncolor] forState:UIControlStateNormal];
            [self.Donebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }else if (idx==1){
            self.forgetbutton = button;
            [self.forgetbutton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
            self.forgetbutton.frame=CGRectMake(RECRWITCH-95, 70*2+62, 70, 30);
            [self.forgetbutton setTitleColor:Buttoncolor forState:UIControlStateNormal];
        }else if (idx==2){
            self.regionbutton = button;
            [self.regionbutton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
            self.regionbutton.layer.borderColor=Buttoncolor.CGColor;
            self.regionbutton.layer.borderWidth=1.5;
            self.regionbutton.frame=CGRectMake(RECRWITCH/2-50, 70*2+100, 100, 30);
            [self.regionbutton setTitleColor:Buttoncolor forState:UIControlStateNormal];
            //            [self.regionbutton setBackgroundImage:[JonesClass createImageWithColor:Buttoncolor] forState:UIControlStateHighlighted];
        }
    }];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    //    if ([JonesClass checkiphone:self.textname.text]&&[JonesClass checkPassWord:self.textpwd.text]) {
    //        [self.Donebutton setBackgroundImage:[JonesClass createImageWithColor:Buttoncolor] forState:UIControlStateNormal];
    //    }else{
    //        [self.Donebutton setBackgroundImage:[JonesClass createImageWithColor:[UIColor grayColor]] forState:UIControlStateNormal];
    //    }
}

-(void)go_done:(UIButton *)sender{
    if (sender.tag==10) {
        
        if (self.textname.text.length<1||self.textname.text==nil) {
            [SVProgressHUD showErrorWithStatus:@"请填写手机号"];
        }else if (self.textpwd.text.length<1||self.textpwd.text==nil){
            [SVProgressHUD showErrorWithStatus:@"请填写密码"];
        }else if (![HuaHereClass checkiphone:self.textname.text]){
            [SVProgressHUD showErrorWithStatus:@"请填写正确的手机号"];
        }else if (![JonesClass checkPassWord:self.textpwd.text]){
            [SVProgressHUD showErrorWithStatus:@"密码格式不对"];
        }else{

            HLoginViewModel *loginvm=[[HLoginViewModel alloc] init:@{@"phone":self.textname.text,@"password":[JonesClass md5:self.textpwd.text]}];
            loginvm.delegate=self;
            
        }
        
    } else if (sender.tag==11){
        HiRegionController *region = [HiRegionController new];
        region.Type=0;
        [self.navigationController pushViewController:region animated:YES];
    } else if (sender.tag==12){
        HiRegionController *region = [HiRegionController new];
        region.Type=1;
        [self.navigationController pushViewController:region animated:YES];
    }
}

-(void)Succes:(int)value{
    if (value==1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTOLOGIN object:@{@"done":@"0"}];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

@end
